###################################################
# Party Fragmentation and Campaign Spending: A
# Subnational Analysis of the German Party System
# Supplemental Material
#
# Marc S. Jacob & Jan Pollex
# Party Politics
###################################################

# Set working directory ---------------------------------------------------

setwd(
  "C:/Users/majacob/Dropbox/Jan_Marc_paper/Party Politics/Final/replication"
)

# Load libraries ----------------------------------------------------------

library(tidyverse)
library(reshape2)
library(ggpubr)
library(sjPlot)
library(gridExtra)
library(stargazer)
library(cowplot)
library(glmmTMB)

# Table A1 ----------------------------------------------------------------

data <- read.csv(file = "data_regression.csv", na.strings = c("", "NA"))

data %>% dplyr::select(
  year,
  e_staff,
  e_mat,
  e_pol_lab,
  e_camp,
  state_gov,
  federal_gov,
  state_el,
  gdp,
  inh,
  ENP,
  i_fees,
  i_don_ind,
  i_don_cor
) -> descriptives

stargazer(
  descriptives,
  type = "text",
  covariate.labels = c(
    "Year",
    "Expenses: Staff",
    "Expenses: Material Costs",
    "Expenses: Political Labor",
    "Expenses: Campagining",
    "State Government",
    "Federal government",
    "State election",
    "GDP in Million Euros",
    "Inhabitants in 1000 Persons",
    "ENEP",
    "Income: Fees",
    "Income: Individual Donations",
    "Income: Corporative Donations"
  ),
  out="descriptives.html"
)

# Figure A1 ---------------------------------------------------------------

data$income <- data$i_fees + data$i_don_ind + data$i_don_cor
data$camp_10000 <- data$e_camp / 10000
data$log_camp <- log(data$e_camp)

# set variables to factors
data$fed_opp <-
  car::recode(data$federal_gov, "0=1;1=0", as.factor = TRUE)
data$state_gov <- as.factor(data$state_gov)
data$state_el <- as.factor(data$state_el)
data$federal_el <- as.factor(data$federal_el)

# round DV (for negative binomial model)
data$e_camp <- round(data$e_camp, digits = 0)

# rescale IVs

rescale <- function(x)
  (x - min(x)) / (max(x) - min(x)) * 100

data$income <-  rescale(data$income)
data$income <- round(data$income, digits = 0)

# transform ENP

data$ENPx10 <-
  data$ENP * 10 # multiply ENP times 10 to receive integers
data$ENPx10 <- round(data$ENPx10, digits = 0)

hist1 <-
  ggplot(data, aes(x = camp_10000)) + geom_histogram(bins = 500) + labs(x =
                                                                          "Campaign spending in 10k Euro", y = "Count") + theme_bw()

hist2 <-
  ggplot(data, aes(x = log_camp)) + geom_histogram(bins = 30) + labs(x =
                                                                       "Log(Campaign spending in Euro)", y = "Count") + theme_bw()

png(file = "histogram.png",
    width = 900,
    height = 500)

plot_grid(
  hist1,
  hist2,
  labels = c("a)", "b)"),
  ncol = 2,
  nrow = 1
)

dev.off()

# Figure B1 ---------------------------------------------------------------

data1 <- data  %>% 
  unite(state_party, state, party, sep = "_")

staff <- ggplot(data=data1, aes(x=year, y=e_staff, group=state_party)) +  geom_line()+  geom_point() + theme_light() + labs(y = "Staff payrolls", x = "Years") + scale_x_continuous(breaks=c(2009,2011,2013,2015,2017))
mat <- ggplot(data=data1, aes(x=year, y=e_mat, group=state_party)) +  geom_line()+  geom_point() + theme_light() + labs(y = "Material costs", x = "Years") + scale_x_continuous(breaks=c(2009,2011,2013,2015,2017))
lab <- ggplot(data=data1, aes(x=year, y=e_pol_lab, group=state_party)) +  geom_line()+  geom_point() + theme_light() + labs(y = "Political labour", x = "Years") + scale_x_continuous(breaks=c(2009,2011,2013,2015,2017))
camp <- ggplot(data=data1, aes(x=year, y=e_camp, group=state_party)) +  geom_line()+  geom_point() + theme_light() + labs(y = "Campaigning", x = "Years") + scale_x_continuous(breaks=c(2009,2011,2013,2015,2017))
staff

png(file = "descr.png",
    width = 900,
    height = 600)

cowplot::plot_grid(
  staff,
  mat,
  lab,
  camp,
  labels = c("a)","b)","c)","d)"),
  ncol = 2,
  nrow = 2
)

dev.off()

# Table C1 ----------------------------------------------------------------

# State election years
e_camp_election <- aggregate(data$e_camp,by=list(data$party,data$state,data$state_el),mean)
e_camp_ENP <- aggregate(data$ENP,by=list(data$party,data$state),mean)

data_wide <- dcast(e_camp_election, Group.1 + Group.2 ~ Group.3, value.var="x",drop=T)

data_wide$ratio<-data_wide$`1`/(data_wide$`0`+ data_wide$`1`)
merged <- merge(data_wide,e_camp_ENP,by=c("Group.1","Group.2"))
merged$ENEP <- merged$x

state <- ggplot(merged, aes(x=ratio, y=ENEP,shape=Group.1)) + geom_point() +  geom_smooth(method=lm,aes(group=1)) +ylab("Average ENEP")+xlab("Ratio state election year spending") + labs(shape = "Party")+ theme_bw()

state_result <-  lm(ratio~ENEP+factor(Group.1)+factor(Group.2), data=merged)
summary(state_result)

# Federal election years
e_camp_election <- aggregate(data$e_camp,by=list(data$party,data$state,data$federal_el),mean)
e_camp_ENP <- aggregate(data$ENP,by=list(data$party,data$state),mean)

e_camp_election$ENP <- e_camp_ENP$x

data_wide <- dcast(e_camp_election, Group.1 + Group.2 ~ Group.3, value.var="x",drop=T)

data_wide$ratio<-data_wide$`1`/(data_wide$`0`+data_wide$`1`)
merged <- merge(data_wide,e_camp_ENP,by=c("Group.1","Group.2"))
merged$ENEP <- merged$x

federal <- ggplot(merged, aes(x=ratio, y=ENEP,shape=Group.1)) + geom_point() +  geom_smooth(method=lm,aes(group=1)) +ylab("Average ENEP")+xlab("Ratio federal election year spending") + labs(shape = "Party")+ theme_bw()

federal_result <-  lm(ratio~ENEP+factor(Group.1)+factor(Group.2), data=merged)
summary(federal_result)

# Make table

label <- c(
  `(Intercept)` = "Intercept",
  `factor(Group.1)CDU`  = "CDU",
  `factor(Group.1)FDP`  = "FDP",
  `factor(Group.1)Greens`  = "Greens",
  `factor(Group.1)Left`  = "Left",
  `factor(Group.1)SPD`  = "SPD",
  `factor(Group.2)BE`  = "BE",
  `factor(Group.2)BW`  = "BW",
  `factor(Group.2)BY`  = "BY",
  `factor(Group.2)HB`  = "HB",
  `factor(Group.2)HE`  = "HE",
  `factor(Group.2)HH`  = "HH",
  `factor(Group.2)MV`  = "MV",
  `factor(Group.2)NI`  = "NI",
  `factor(Group.2)NW`  = "NW",
  `factor(Group.2)RP`  = "RP",
  `factor(Group.2)SH`  = "SH",
  `factor(Group.2)SL`  = "SL",
  `factor(Group.2)SN`  = "SN",
  `factor(Group.2)ST`  = "ST",
  `factor(Group.2)TH`  = "TH")

## Table models

tab_model(
  state_result,
  federal_result,
  show.ci = 0.95,
  show.p = FALSE,
  transform = NULL,
  show.est = T,
  show.se = TRUE,
  collapse.se = TRUE,
  digits = 3,
  p.style = "stars",
  dv.labels = c(
    "Ratio State Election Years",
    "Ratio Federal Election Years"
  ),
  pred.labels = label
)

# Figure D1/D2 ------------------------------------------------------------

model_camp2 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = data
  )

plots <- plot_model(model_camp2, type = "re")

png(file = "B1.png",
    width = 500,
    height = 1000)

plots[[1]] 

dev.off()

png(file = "B2.png",
    width = 500,
    height = 600)

plots[[2]] 

dev.off()

png(file = "B3.png",
    width = 500,
    height = 600)

plots[[3]] 

dev.off()

# Table E1 ----------------------------------------------------------------

# build dataframe without AfD and The Left observations
datamAfD <- data[!(data$party == "AfD"),]
datamLeft <- data[!(data$party == "Left"),]
datamSPD <- data[!(data$party == "SPD"),]
datamCDU <- data[!(data$party == "CDU"),]
datamGreens <- data[!(data$party == "Greens"),]
datamFDP <- data[!(data$party == "FDP"),]

m1 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = datamCDU
  )

m2 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = datamSPD
  )

m3 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = datamGreens
  )

m4 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = datamFDP
  )

m5 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = datamLeft
  )

m6 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = datamAfD
  )


pl <- c(
  `(Intercept)` = "Intercept",
  ENPx10 = "ENEPx10",
  state_gov1 = "State government",
  federal_gov = "Federal government",
  state_el1 = "State election",
  federal_el1 = "Federal election",
  income = "Income",
  `ENPx10:state_el1` = "(ENEPx10)xState election",
  `ENPx10:federal_el1` = "(ENEPx10)xFederal election"
)

# Table models
tab_model(
  m1,
  m2,
  m3,
  m4,
  m5,
  m6,
  show.ci = FALSE,
  show.p = FALSE,
  transform = NULL,
  show.std = "std2",
  show.zeroinf = TRUE,
  show.est = FALSE,
  show.se = TRUE,
  collapse.se = TRUE,
  digits = 3,
  p.style = "stars",
  dv.labels = c(
    "Model 1 (without CDU)",
    "Model 2 (without SPD)",
    "Model 3 (without Greens)",
    "Model 4 (without FDP)",
    "Model 5 (without Left)",
    "Model 6 (without AfD)"
  ),
  pred.labels = pl
)

# Table F1 ----------------------------------------------------------------

data$inh_re <- rescale(data$inh) # rescaling
data$gdp_re <- rescale(data$gdp) # rescaling

model_camp2 <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + income +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = data
  )


minh <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + inh_re +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = data
  )

mgdp <-
  glmmTMB(
    e_camp ~ ENPx10 + state_gov + federal_gov + state_el + federal_el + ENPx10 *
      state_el + ENPx10 * federal_el
    + gdp_re +  (1 |
                   state:party) + (1 | year) + (1 | state),
    family = nbinom2(link = "log"),
    REML = FALSE,
    data = data
  )

pl <- c(
  `(Intercept)` = "Intercept",
  ENPx10 = "ENEPx10",
  state_gov1 = "State government",
  federal_gov = "Federal government",
  state_el1 = "State election",
  federal_el1 = "Federal election",
  income = "Income",
  `ENPx10:state_el1` = "(ENEPx10)xState election",
  `ENPx10:federal_el1` = "(ENEPx10)xFederal election",
  `inh_re` = "State Inhabitants (rescaled from 0 to 100)",
  `gdp_re` = "State GDP (rescaled from 0 to 100)"
)

# Table models
tab_model(
  model_camp2,
  minh,
  mgdp,
  show.ci = FALSE,
  show.p = FALSE,
  transform = NULL,
  show.std = "std2",
  show.zeroinf = TRUE,
  show.est = FALSE,
  show.se = TRUE,
  collapse.se = TRUE,
  digits = 3,
  p.style = "stars",
  dv.labels = c(
    "Model 1 (Branch Income)",
    "Model 2 (State Inhabitants)",
    "Model 3 (State GDP)"
  ),
  pred.labels = pl
)
